<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="right-panel-search">
					<el-input v-model="search.patient_sn" placeholder="问诊单号" clearable
							  style="min-width:15%;"></el-input>
					<el-select v-model="search.provider_id" placeholder="服务商" style="min-width:15%;" clearable>
						<el-option v-for="item in provider_list" :key="item.provider_id" :label="item.provider_name"
								   :value="item.provider_id">
						</el-option>
					</el-select>
					<el-input v-model="search.user_mobile" placeholder="账号" clearable
							  style="min-width:15%;"></el-input>
					<el-input v-model="search.doctor_name" placeholder="接诊医生" clearable
							  style="min-width:15%;"></el-input>
					<el-input v-model="search.patient_name" placeholder="就诊人" clearable
							  style="min-width:15%;"></el-input>
					<el-input v-model="search.product_name" placeholder="商品名称" clearable
							  style="min-width:15%;"></el-input>
				</div>
			</el-header>
			<el-header>
				<div class="right-panel-search">
					<el-input v-model="search.company_name" placeholder="单位名称" clearable
							  style="min-width:15%;width:200px;"></el-input>
					<el-select v-model="search.status" placeholder="问诊状态" style="min-width:15%;" clearable>
						<el-option v-for="item in statusList" :key="item.status" :label="item.status_name"
								   :value="item.status">
						</el-option>
					</el-select>
					<el-date-picker
						style="min-width:25%;"
						v-model="dateTo"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
						clearable="true"
					/>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="info" @click="resetsearch">重置</el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" :params="search" stripe
						 remoteSort
						 remoteFilter>
					<!--					<el-table-column type="selection" width="50"></el-table-column>-->
					<!--					<el-table-column label="ID" prop="company_id" width="100"></el-table-column>-->
					<el-table-column label="问诊单号" prop="patient_sn" width="200"></el-table-column>
					<el-table-column label="账号" prop="user_mobile" width="150"></el-table-column>
					<el-table-column label="单位名称" prop="company_name" width="150"></el-table-column>
					<el-table-column label="商品名称" prop="product_name" width="150"></el-table-column>
					<el-table-column label="就诊人" prop="patient_name" width="150"></el-table-column>
					<el-table-column label="接诊医生" prop="doctor_name" width="150"></el-table-column>
					<el-table-column label="问诊状态" prop="status_name" width="150"></el-table-column>
					<el-table-column label="服务商" prop="provider_name"
									 width="150"></el-table-column>
					<el-table-column label="监管同步状态" prop="sync_status_name" width="150"></el-table-column>
					<el-table-column label="问诊时间" prop="created_at" width="150"></el-table-column>
					<el-table-column label="通话开始时间" prop="call_begin" width="150"></el-table-column>
					<el-table-column label="通话结束时间" prop="call_end" width="150"></el-table-column>
					<el-table-column label="问诊完成时间" prop="over_at" width="150"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row)"
										   v-auth="'patient:order:info'">查看问诊单
								</el-button>
								<el-button v-show="scope.row.is_sync == 1" text type="primary" size="small"
										   @click="syncOrder(scope.row)"
										   v-auth="'patient:order:syncOrder'">同步监管
								</el-button>
								<el-button style="color: #ccc;" v-show="scope.row.is_sync == 0" text type="primary" size="small"
										   v-auth="'patient:order:syncOrder'">同步监管
								</el-button>
								<el-button v-show="scope.row.sync_status != 4" text type="primary" size="small"
										   @click="toLog(scope.row)"
										   v-auth="'patient:order:toLog'">同步日志
								</el-button>
								<el-button style="color: #ccc;" v-show="scope.row.sync_status == 4" text type="primary" size="small"
										   v-auth="'patient:order:toLog'">同步日志
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './patient_order_detail.vue'

export default {
	name: 'order',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			showGroupLoading: false,
			loading: false,
			apiObj: this.$API['patient.patient_order'].index,
			selection: [],
			statusList: [
				{status: 0, status_name: "全部"},
				{status: 2, status_name: "问诊中"},
				{status: 3, status_name: "已完成"},
				{status: 4, status_name: "已取消"},
			],
			channelList:[
				{channel_id: '0', channel_name: "全部"},
				{channel_id: 'ZHIYING', channel_name: "直营"},
				{channel_id: 'YIAN', channel_name: "怡安医疗"},
				{channel_id: 'HEHUAN', channel_name: "和缓医疗"},
			],
			provider_list:[],
			companyList: [],
			//时间搜索
			dateTo: [
				this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
				this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 23:59:59',
			],
			search: {
				patient_sn:null,
				channel_provider:null,
				user_mobile:null,
				patient_id:this.$route.query.patient_id,
				patient_name: this.$route.query.patient_name,
				doctor_name:null,
				company_name:null,
				status:null,
				created_at_begin: this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
				created_at_end: this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 23:59:59',
				not_status: 1,
				provider_id:'',
				product_name:'',
			},
		}
	},
	watch: {
		dateTo(val) {
			if (val) {
				this.search.created_at_begin = val[0];
				this.search.created_at_end = val[1];
			}else{
				this.search.created_at_begin = '';
				this.search.created_at_end = '';
			}
		},
	},
	mounted() {
		this.getProviderList();
	},
	methods: {
		//删除
		syncOrder(row) {
			this.$confirm(`注：确认同步监管吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['patient.patient_order'].syncOrder.post({patient_sn: row.patient_sn})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.dateTo = [];
			this.search = {
				not_status: 1,
				patient_id:0,
				patient_sn:null,
				channel_provider:null,
				user_mobile:null,
				patient_name: null,
				doctor_name:null,
				company_name:null,
				status:null,
				created_at_begin: null,
				created_at_end: null,
				provider_id:'',
				product_name:'',
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess() {
			this.dialog.save = false
			this.$refs.table.reload()
		},
		//查看
		table_show(row) {
			this.$router.push({
				path: '/patient/patient_order_detail',
				query: {
					patient_sn: row.patient_sn,
					action: 'view'
				}
			})
		},
		//同步日志
		toLog(row) {
			this.$router.push({
				path: '/patient/patient_order_sync',
				query: {
					patient_sn: row.patient_sn,
					action: 'view'
				}
			})
		},
		getProviderList() {
			this.$API['patient.patient_order'].serviceProvider.get().then((res) => {
				if (res.code == 0) {
					this.provider_list = res.data;
					this.provider_list.unshift({'provider_id':0,'provider_name':'全部'});
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
	}
}
</script>
